﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingsApp.Model;
using TrainingsApp.DAL;
using System.Web.UI.WebControls;

namespace TrainingsApp.Logic {
    public class Sys_Permission_Logic {
        #region 功能管理
        public static T_FUNCTION_Entity T_FUNCTION_View(System.Int64 ID) {
            return Sys_Permission_DAL.T_FUNCTION_View(ID);
        }

        public static T_FUNCTION_Entity T_FUNCTION_Add(T_FUNCTION_Entity entity) {
            return Sys_Permission_DAL.T_FUNCTION_Add(entity);
        }

        public static void T_FUNCTION_Update(T_FUNCTION_Entity entity) {
            Sys_Permission_DAL.T_FUNCTION_Update(entity);
        }

        public static void T_FUNCTION_Delete(System.Decimal ID) {
            Sys_Permission_DAL.T_FUNCTION_Delete(ID);
        }

        //public static int T_FUNCTION_RecordCount(Dictionary<string, object> parameters)
        //{
        //    return Sys_Permission_DAL.T_FUNCTION_RecordCount(parameters);
        //}

        public static List<T_FUNCTION_Entity> T_FUNCTION_ListByPaging(Dictionary<string, object> parameters, int pageSize, int pageIndex, out int recordCount) {
            return Sys_Permission_DAL.T_FUNCTION_ListByPaging(parameters, pageSize, pageIndex, out recordCount);
        }

        /// <summary>
        /// 父功能列表
        /// </summary>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_FUNCTION_GetParentFunction() {
            return Sys_Permission_DAL.T_FUNCTION_GetParentFunction();
        }

        /// <summary>
        /// 功能列表
        /// </summary>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_FUNCTION_GetAllFunction() {
            return Sys_Permission_DAL.T_FUNCTION_GetAllFunction();
        }

        /// <summary>
        /// 根据用户取功能列表
        /// </summary>
        /// <returns></returns>
        public static List<T_FUNCTION_Entity> T_Function_ListByUserId(System.Int64 UserId) {
            return Sys_Permission_DAL.T_Function_ListByUserId(UserId);
        }
        #endregion

        #region 角色管理

        public static T_ROLE_Entity T_ROLE_View(System.Decimal ID) {
            return Sys_Permission_DAL.T_ROLE_View(ID);
        }

        public static T_ROLE_Entity T_ROLE_Add(T_ROLE_Entity entity) {
            return Sys_Permission_DAL.T_ROLE_Add(entity);
        }

        public static void T_ROLE_Update(T_ROLE_Entity entity) {
            Sys_Permission_DAL.T_ROLE_Update(entity);
        }

        public static void T_ROLE_Delete(System.Decimal ID) {
            Sys_Permission_DAL.T_ROLE_Delete(ID);
        }

        /// <summary>
        /// 角色记录数
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        //public static int T_Role_RecordCount(Dictionary<string, object> parameters)
        //{
        //    return Sys_Permission_DAL.T_Role_RecordCount(parameters);
        //}

        /// <summary>
        /// 角色分页列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<T_ROLE_Entity> T_Role_ListByPaging(Dictionary<string, object> parameters, int pageSize, int pageIndex, out int recordCount) {
            return Sys_Permission_DAL.T_Role_ListByPaging(parameters, pageSize, pageIndex, out recordCount);
        }

        /// <summary>
        /// 角色全列表
        /// </summary>
        /// <returns></returns>
        public static List<T_ROLE_Entity> T_Role_GetAllRole() {
            return Sys_Permission_DAL.T_Role_GetAllRole();
        }

        /// <summary>
        /// 根据用户获取角色
        /// </summary>
        /// <returns></returns>
        public static List<T_ROLE_Entity> T_Role_ListByUserId(System.Int64 UserId) {
            return Sys_Permission_DAL.T_Role_ListByUserId(UserId);
        }
        #endregion

        #region 角色功能关系
        /// <summary>
        /// 更新角色功能
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static void T_Role_Function_Update(System.Decimal RoleId, List<T_ROLE_FUNCTION_Entity> list) {
            Sys_Permission_DAL.T_Role_Function_DeleteByRoleId(RoleId);
            for (int i = 0; i < list.Count; i++) {
                Sys_Permission_DAL.T_Role_Function_Add(list[i]);
            }
        }

        /// <summary>
        /// 根据角色获取功能
        /// </summary>
        /// <param name="RoleId"></param>
        public static List<T_ROLE_FUNCTION_Entity> T_Role_Function_ListByRoleId(System.Decimal RoleId) {
            return Sys_Permission_DAL.T_Role_Function_ListByRoleId(RoleId);
        }

        /// <summary>
        /// 根据用户获取功能
        /// </summary>
        /// <param name="UserId"></param>
        public static List<T_ROLE_FUNCTION_Entity> T_Role_Function_ListByUserId(System.Decimal UserId) {
            return Sys_Permission_Logic.T_Role_Function_ListByUserId(UserId);
        }
        #endregion

        #region 用户管理
        public static TS_User_Entity T_USER_View(System.Int64 ID) {
            return Sys_Permission_DAL.T_USER_View(ID);
        }

        public static TS_User_Entity T_USER_View(System.String UserName) {
            return Sys_Permission_DAL.T_USER_View(UserName);
        }

        public static TS_User_Entity T_USER_Add(TS_User_Entity entity) {
            return Sys_Permission_DAL.T_USER_Add(entity);
        }

        public static void T_USER_Update(TS_User_Entity entity) {
            Sys_Permission_DAL.T_USER_Update(entity);
        }

        public static void T_USER_Delete(System.Decimal ID) {
            Sys_Permission_DAL.T_USER_Delete(ID);
        }

        /// <summary>
        /// 用户记录数
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        //public static int T_USER_RecordCount(Dictionary<string, object> parameters)
        //{
        //    return Sys_Permission_DAL.T_USER_RecordCount(parameters);
        //}

        /// <summary>
        /// 用户分页列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<TS_User_Entity> T_User_ListByPaging(Dictionary<string, object> parameters, int pageSize, int pageIndex, out int recordCount) {
            return Sys_Permission_DAL.T_User_ListByPaging(parameters, pageSize, pageIndex, out  recordCount);
        }
        #endregion

        #region 用户角色关系
        /// <summary>
        /// 更新用户角色
        /// </summary>
        /// <param name="YserId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static void T_USER_Role_Update(System.Decimal UserId, List<TS_USER_ROLE_Entity> list) {
            Sys_Permission_DAL.T_USER_Role_DeleteByUserId(UserId);
            for (int i = 0; i < list.Count; i++) {
                Sys_Permission_DAL.T_USER_Role_Add(list[i]);
            }
        }

        /// <summary>
        /// 根据用户获取角色
        /// </summary>
        /// <param name="UserId"></param>
        public static List<TS_USER_ROLE_Entity> T_USER_Role_ListByUserId(System.Decimal UserId) {
            return Sys_Permission_DAL.T_USER_Role_ListByUserId(UserId);
        }
        #endregion

        #region 获取用户登录信息
        public static UserContext GetUserInfo(System.String UserName) {
            UserContext user = null;
            TS_User_Entity userInfo = T_USER_View(UserName);
            if (userInfo != null) {
                user = new UserContext();
                List<T_ROLE_Entity> roleInfo = Sys_Permission_DAL.T_Role_ListByUserId(userInfo.Idx);
                List<T_FUNCTION_Entity> funcInfo = Sys_Permission_DAL.T_Function_ListByUserId(userInfo.Idx);

                user.RealName = userInfo.Name;
                user.LoginTime = DateTime.Now;
                user.IdentityCard = userInfo.IdentityCard;
                user.Mobile = userInfo.Mobile;
                user.Email = userInfo.Email;
                user.Roles = roleInfo;
                user.FunctionRights = funcInfo;
                user.UserName = userInfo.UserName;
                user.UserID = userInfo.Idx;
                user.UserType = (UserType)(userInfo.UserType ?? 128);

                //Sys_Permission_DAL.T_USER_Update(userInfo);
            }
            return user;
        }

        public static UserContext UserLogin(System.String Account, System.String Pwd) {
            UserContext user = null;
            TS_User_Entity userInfo = T_USER_View(Account);
            if (userInfo != null && string.Compare(userInfo.Password ?? "", Pwd, true) == 0) {
                user = new UserContext();

                user.RealName = userInfo.Name;
                user.LoginTime = DateTime.Now;
                user.UserName = Account;
                user.UserName = userInfo.UserName;

                //Sys_Permission_DAL.T_USER_Update(userInfo);
            }
            return user;
        }

        #endregion
    }
}
